package com.css.msgcenter.admin.msglog.action;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.db.page.Page;
import com.css.msgcenter.server.QueryMsg;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

public class DirMsgTask extends AdminAction{
	private static Log log = LogFactory.getLog(DirMsgTask.class);
	private String sender;
	private String receiver;
	private String msgStatus;
	private String appId;
	private String msgType;
	private Date beginTime;
	private Date endTime;
	private Page page;
	
	public DirMsgTask(){
		page = new Page();
		page.setCountField("a.uuid");
	}
	
	@Override
	protected String adminGo() {
		try{
			QueryMsg qm = new QueryMsg("select a.uuid, b.sender, a.receiver_name, a.msg_type, b.app_id, a.create_time, a.begin_time, a.finish_time, a.fail_count, a.msg_status " 
					+ "from msg_task a left join msg_main b on a.msg_id=b.uuid " 
					+ getWhere() + getOrder(), true);
			setWhere(qm);
			page = qm.page(page);
			return Action.SUCCESS;
		}catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}
	
	public String getWhere() {
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		if(StringHelper.isNotEmpty(sender))
			sb.append(" and b.sender like :sender ");
		if(StringHelper.isNotEmpty(receiver))
			sb.append(" and a.receiver_name like :receiver ");
		if(StringHelper.isNotEmpty(msgStatus))
			sb.append(" and a.msg_status =:msgStatus ");
		if(StringHelper.isNotEmpty(appId))
			sb.append(" and b.app_id =:appId ");
		if(StringHelper.isNotEmpty(msgType))
			sb.append(" and a.msg_type =:msgType ");
		if(beginTime != null)
			sb.append(" and a.create_time >=:beginTime ");
		if(endTime != null)
			sb.append(" and a.createTime <:endTime ");
 		return sb.toString();
	}
	public void setWhere(QueryMsg qm) {
		if (StringHelper.isNotEmpty(sender))
			qm.setParameter("sender", "%" + sender.trim() + "%");
		if(StringHelper.isNotEmpty(receiver))
			qm.setParameter("receiver", "%" + receiver.trim() + "%");
		if(StringHelper.isNotEmpty(msgStatus))
			qm.setParameter("msgStatus",msgStatus);
		if(StringHelper.isNotEmpty(appId))
			qm.setParameter("appId",appId);
		if(StringHelper.isNotEmpty(msgType))
			qm.setParameter("msgType",msgType);
		if(beginTime != null)
			qm.setParameter("beginTime",beginTime);
		if(endTime!= null){
			Calendar cal = Calendar.getInstance();
	        cal.setTime(endTime);
	        cal.add(Calendar.DATE, 1);
	        qm.setParameter("endTime",cal.getTime());
		}
		
	}
	public String getOrder() {
		return StringHelper.isNotEmpty(page.getOrderByString()) ? page.getOrderByString() : " order by a.create_time desc ";
	}

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getMsgType() {
		return msgType;
	}

	public void setMsgType(String msgType) {
		this.msgType = msgType;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getMsgStatus() {
		return msgStatus;
	}

	public void setMsgStatus(String msgStatus) {
		this.msgStatus = msgStatus;
	}

}
